const cheerio = require("cheerio")
//连接数据库
const connection = require('./database')
const { default: axios } = require("axios")
const insert_sql = `INSERT INTO base_cnarea (pid,code,name,level) VALUES(?,?,?,?);`
axios.defaults.baseURL = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/'
var demo = 0
async function start() {
    const mainPage = await axios.get('index.html')
    const $mainDom = cheerio.load(mainPage.data, {
        normalizeWhitespace: false,
        xmlMode: false,
        decodeEntities: true
    });
    // 获取省
    // 省的dom
    const provinceDom = $mainDom(".provincetr a")
    // 省的集合 包含 名字  url
    const provinceList = []
    provinceDom.each(async function (i, ele) {
        provinceList.push({ name: $mainDom(this).text(), href: $mainDom(this)[0].attribs.href })
    })
    // 遍历省份
    for (let Pi = 0; Pi < provinceList.length; Pi++) {
        const provinceItem = provinceList[Pi]
        // 省名字
        const provinceName = provinceItem.name
        // 省代码
        var provinceCode
        // 省ID
        var provinceId
        // 获取市
        const cityPage = await axios.get(provinceItem.href)
        const $cityDom = cheerio.load(cityPage.data, {
            normalizeWhitespace: false,
            xmlMode: false,
            decodeEntities: true
        })
        // 市的集合 包含 名字 code url
        const cityList = []
        $cityDom(".citytr").each(async function (i, ele) {
            const cityData = {
                name: $cityDom(this).children().eq(1).text(),
                code: $cityDom(this).children().eq(0).text().slice(0, 6),
                href: $cityDom(this).children().eq(0).children().eq(0).attr("href")
            }
            cityList.push(cityData)
        })
        // 遍历市
        for (let i = 0; i < cityList.length; i++) {
            const cityItem = cityList[i];
            if (i === 0) {
                // 保存省
                provinceCode = cityItem.code.slice(0, 3) + '000'
                const [provinceResult] = await connection.query(insert_sql, [null, provinceCode, provinceName, 0])
                provinceId = provinceResult.insertId
            }
            // 保存市
            const [provinceResult] = await connection.query(insert_sql, [provinceId, cityItem.code, cityItem.name, 1])
            // 市的id
            const cityId = provinceResult.insertId
            // 获取区
            const areaPage = await axios.get(cityItem.href)
            const $areaDom = cheerio.load(areaPage.data, {
                normalizeWhitespace: false,
                xmlMode: false,
                decodeEntities: true
            })

            // 区的集合 包含 名字  code
            const areaList = []
            $areaDom(".countytr").each(function (i, ele) {
                const areaData = {
                    name: $areaDom(this).children().eq(1).text(),
                    code: $areaDom(this).children().eq(0).text().slice(0, 6),
                    href: $areaDom(this).children().eq(0).children().eq(0).attr("href"),
                    cityId
                }
                areaList.push(areaData)
            })
            // 遍历区
            for (let i = 0; i < areaList.length; i++) {
                const areaItem = areaList[i];
                await connection.query(insert_sql, [cityId, areaItem.code, areaItem.name, 2])
                if (areaList.length === (i + 1)) {
                    console.log(provinceId, provinceName)
                }
            }
        }
    }
}

start()
